home *** CD-ROM | disk | FTP | other *** search
/ AMIGA-CD 2 / Amiga-CD - Volume 2.iso / ungepackte_daten / 1995 / 7 / 02 / tips.ampk / Tips&Tricks / MessHz.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-01  |  5.8 KB  |  200 lines

  1. #include <intuition/screens.h>
  2. #include <intuition/intuitionbase.h>
  3. #include <intuition/intuition.h>
  4. #include <graphics/modeid.h>
  5. #include <dos/dostags.h>
  6. #include <exec/libraries.h>
  7. #include <libraries/asl.h>
  8. #include <clib/intuition_protos.h>
  9. #include <clib/graphics_protos.h>
  10. #include <clib/exec_protos.h>
  11. #include <clib/dos_protos.h>
  12.  
  13. extern struct IntuitionBase *IntuitionBase;
  14.  
  15. struct Library *TimerBase;
  16.  
  17. #define SCREENTITLE "David Göhlers visuelles Frequenz-Meßgerät © 1995"
  18. #define MINHERTZ 10
  19. #define MAXHERTZ 90
  20. #define MINDELAY 4000
  21. #define MAXDELAY 100000
  22. #define PROCNAME "MessHzToggleColor"
  23.  
  24. const char version[] = "$VER: MessHz 1.0 © David Göhler (24.05.95)";
  25.  
  26. int hertz       = 600;      // Hertz mal 10
  27. ULONG longdelay = 14166;    // wartezeit in µs - (500 + TimeDelay-Overhead)
  28. ULONG MySignal;
  29. struct ViewPort *vp = 0;
  30.  
  31. /*
  32.  * GetAmiTime returns a LONG value. It's the system time in
  33.  * milliseconds. If the value returned is -1, something went wrong.
  34.  */
  35.  
  36. LONG GetAmiTime(void)
  37. {
  38.    struct timeval time = {0,0};      /* tv_secs & tv_micro */
  39.    struct timerequest timereq = {0};
  40.    LONG retcode = -1;
  41.  
  42.    if (!OpenDevice(TIMERNAME,UNIT_ECLOCK,(struct IORequest *)&timereq,0L))
  43.    {  TimerBase = (struct Library *)timereq.tr_node.io_Device;
  44.  
  45.       GetSysTime(&time);
  46.  
  47.       retcode = (time.tv_secs)*1000000 + (time.tv_micro);
  48.       CloseDevice((struct timerequest *)&timereq);
  49.    }
  50.  
  51.    return retcode;
  52. }
  53.  
  54. void HertzAus(struct Screen *s, struct Window *w)
  55. {
  56.    char textaus[30];
  57.  
  58.    Move(w->RPort, 20,s->BarHeight+20);
  59.    sprintf(textaus,"%02ld,%1ld Hertz",hertz/10,hertz%10);
  60.    Text(w->RPort,textaus, strlen(textaus));
  61.  
  62.    Move(w->RPort, 20,s->BarHeight+40);
  63.    sprintf(textaus,"%02ld LongDelay",longdelay);
  64.    Text(w->RPort,textaus, strlen(textaus));
  65. }
  66.  
  67. #define LOOPCOUNT 40
  68.  
  69. int CheckTimeDelay()
  70. {
  71.   int i = LOOPCOUNT;
  72.   ULONG million = 1000000;
  73.   ULONG shortdelay = 500;
  74.   ULONG bla;
  75.   LONG messtime;
  76.  
  77.   messtime = GetAmiTime();
  78.   while ((i--) && (!CheckSignal(MySignal)))
  79.   {  bla = (10 * million / (shortdelay +  shortdelay + shortdelay));
  80.      SetRGB4(vp,0,0,0,0);
  81.      TimeDelay(UNIT_MICROHZ,0,shortdelay);
  82.      SetRGB4(vp,0,10,10,10);
  83.      TimeDelay(UNIT_MICROHZ,0,shortdelay);
  84.   }
  85.   return (GetAmiTime() - messtime - (LOOPCOUNT*shortdelay*2)) / LOOPCOUNT;
  86. }
  87.  
  88. __geta4 void ToggleColor0(void)
  89. {
  90.   ULONG million = 1000000;
  91.   ULONG shortdelay = 500; // 1/2000 Sekunde Schaltung
  92.   ULONG messdelay;
  93.  
  94.   MySignal  = 1 << AllocSignal(-1L);
  95.   messdelay = CheckTimeDelay();
  96.   longdelay = (million-((hertz/10)*(messdelay+shortdelay))) / (hertz/10);
  97.  
  98.   while (!CheckSignal(MySignal))
  99.   {  hertz = (10*million / (messdelay + shortdelay + longdelay));
  100.      SetRGB4(vp,0,0,0,0);
  101.      TimeDelay(UNIT_MICROHZ,0,shortdelay);
  102.      SetRGB4(vp,0,10,10,10);
  103.      TimeDelay(UNIT_MICROHZ,0,longdelay);
  104.   }
  105. }
  106.  
  107. struct TagItem ti[] = {
  108.     {  ASLSM_InitialDisplayID, INVALID_ID },
  109.     {  TAG_DONE, 0 }
  110. };
  111.  
  112. int main(int argc, char *argv[])
  113. {
  114.    struct Screen *s = 0L;
  115.    ULONG DisplayID = INVALID_ID;
  116.    BOOL loopy;
  117.  
  118.    s = IntuitionBase->FirstScreen;
  119.    while ((DisplayID = GetScreenMode(s,ti,0)) != INVALID_ID)
  120.    {
  121.       ti[0].ti_Data = DisplayID;
  122.       loopy = TRUE;
  123.       if ( s = OpenScreenTags(0L,SA_DisplayID, DisplayID, TAG_DONE))
  124.       {
  125.          struct Window *w = 0L;
  126.  
  127.          vp = &(s->ViewPort);
  128.          if (w = OpenWindowTags(0L,
  129.                 WA_IDCMP,        IDCMP_RAWKEY,
  130.                 WA_CustomScreen, s,
  131.                 WA_ScreenTitle,  SCREENTITLE,
  132.                 WA_Flags,        WFLG_BORDERLESS | WFLG_BACKDROP |
  133.                                  WFLG_ACTIVATE | WFLG_RMBTRAP,
  134.                 TAG_DONE)
  135.             )
  136.          {  struct IntuiMessage * imsg;
  137.             struct Process *p;
  138.  
  139.             HertzAus(s,w);
  140.             p = CreateNewProcTags(NP_Entry,    ToggleColor0,
  141.                    NP_Priority, 50, // muß hoch sein
  142.                    NP_Name, PROCNAME,
  143.                    TAG_DONE
  144.                   );
  145.             HertzAus(s,w);
  146.             while (loopy)
  147.             {  Wait(1L << w->UserPort->mp_SigBit);
  148.                while (imsg = GetMsg(w->UserPort))
  149.                {  int key = -1;
  150.                   static BOOL shift = FALSE;
  151.                   static BOOL alt   = FALSE;
  152.  
  153.                   if (imsg->Class == IDCMP_RAWKEY)
  154.                   {  key = imsg->Code; }
  155.                   ReplyMsg(imsg);
  156.                   if (key != -1)
  157.                   {
  158.                      switch (key) {
  159.                        case 0x60:
  160.                        case 0x61: shift = TRUE; break;
  161.                        case 0xE0:
  162.                        case 0xE1: shift = FALSE; break;
  163.                        case 0x64:
  164.                        case 0x65: alt = TRUE; break;
  165.                        case 0xE4:
  166.                        case 0xE5: alt = FALSE; break;
  167.                        case 0x45: // ESC
  168.                                   loopy = FALSE;
  169.                                   while (imsg = GetMsg(w->UserPort))
  170.                                   {  ReplyMsg(imsg); }
  171.                                   break;
  172.                        case 0x1B: // + Taste
  173.                                   if (longdelay > MINDELAY)
  174.                                   {  longdelay -= (!(alt+shift))+10*shift +100*alt;
  175.                                   }
  176.                                   break;
  177.                        case 0x3A: // - Taste
  178.                                   if (longdelay < MAXDELAY)
  179.                                   {  longdelay += (!(alt+shift))+10*shift +100*alt;
  180.                                   }
  181.                                   break;
  182.                        default:   break;
  183.                      }
  184.                      HertzAus(s,w);
  185.                   }
  186.                }
  187.             }
  188.             while (FindTask(PROCNAME))
  189.             {  Signal(p,MySignal);
  190.                Delay(10);
  191.             }
  192.             CloseWindow(w);
  193.          }
  194.          CloseScreen(s);
  195.       }
  196.       s = IntuitionBase->FirstScreen;
  197.    }
  198.    return (s == 0);
  199. }
  200.